package com.service.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.admin.base.controller.rep.CoinsRep;
import com.service.admin.base.controller.req.CoinSaveReq;
import com.service.admin.base.service.CoinService;
import com.service.admin.member.enums.MemberEnums;
import com.service.core.exception.CommonException;
import com.service.dao.mapper.CoinsMapper;
import com.service.dao.mapper.MemberMapper;
import com.service.dao.mapper.MemberStoreMapper;
import com.service.dao.model.Coins;
import com.service.dao.model.Member;
import com.service.dao.model.MemberStore;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CoinServiceImpl extends ServiceImpl<CoinsMapper, Coins> implements CoinService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberStoreMapper memberStoreMapper;


    @Override
    public List<CoinsRep> getCoins() {
        return this.list().stream().map(item -> ModelMapperUtil.map(item, CoinsRep.class)).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CoinSaveReq coinSaveReq) {
        Assert.notNull(coinSaveReq.getCoinFee(), "参数【coinFee】不能为空");
        Assert.notNull(coinSaveReq.getCoinName(), "参数【coinName】不能为空");
        Assert.notNull(coinSaveReq.getShowAsset(), "参数【showAsset】不能为空");
        Assert.notNull(coinSaveReq.getTransfer(), "参数【transfer】不能为空");
        Assert.notNull(coinSaveReq.getOutputNum(), "参数【outputNum】不能为空");
        Coins coins = ModelMapperUtil.map(coinSaveReq, Coins.class);
        coins.setCoinNameZh(coinSaveReq.getCoinName());
        List<Member> members = memberMapper.selectList(new QueryWrapper<Member>().lambda()
            .eq(Member::getUserStatus, MemberEnums.Status.ACTIVE.getCode())
        );
        save(coins);
        Coins one = getOne(new LambdaQueryWrapper<Coins>().eq(Coins::getCoinName, coinSaveReq.getCoinName()));
        if (ObjectUtils.isEmpty(one)) {
            throw new CommonException("添加失败");
        }
        members.parallelStream().forEach(item->{
            //获该用户币种
            List<MemberStore> memberStores = memberStoreMapper.selectList(new QueryWrapper<MemberStore>().lambda()
                    .eq(MemberStore::getMemberId, item.getId())
            );
            //遍历改用户U币种
            for (MemberStore memberStore : memberStores) {
                if (memberStore.getCoinsName().equals(coinSaveReq.getCoinName())) {
                    break;
                }
                if (memberStores.size() - 1 == memberStores.indexOf(memberStore)) {
                    initMemberStores(coinSaveReq, one, item);
                }
            }
            if (memberStores.isEmpty()){
                initMemberStores(coinSaveReq, one, item);
            }
        });
    }

    @Override
    public void update(CoinSaveReq coinSaveReq) {
        if (ObjectUtils.isEmpty(getById(coinSaveReq.getId()))) {
            throw new CommonException("无此记录");
        }
        Coins coins = ModelMapperUtil.map(coinSaveReq, Coins.class);
        updateById(coins);
        List<Member> members = memberMapper.selectList(new QueryWrapper<Member>().lambda()
                .eq(Member::getUserStatus, MemberEnums.Status.ACTIVE.getCode())
        );
        members.parallelStream().forEach(item->{
            //获该用户币种
            List<MemberStore> memberStores = memberStoreMapper.selectList(new QueryWrapper<MemberStore>().lambda()
                .eq(MemberStore::getMemberId, item.getId())
            );
            //遍历改用户U币种
            for (MemberStore memberStore : memberStores) {
                if (memberStore.getCoinsName().equals(coinSaveReq.getCoinName())) {
                    break;
                }
                if (memberStores.size() - 1 == memberStores.indexOf(memberStore)) {
                    initMemberStores(coinSaveReq, coins, item);
                }
            }
            if (memberStores.isEmpty()){
                initMemberStores(coinSaveReq, coins, item);
            }
        });
    }


    private void initMemberStores(CoinSaveReq coinSaveReq, Coins one, Member member) {
        MemberStore newMemberStore = new MemberStore();
        newMemberStore.setMemberId(member.getId());
        newMemberStore.setNum(BigDecimal.ZERO);
        newMemberStore.setFrostNum(BigDecimal.ZERO);
        newMemberStore.setCoinsName(coinSaveReq.getCoinName());
        newMemberStore.setCoinsId(one.getId());
        Date now = new Date();
        newMemberStore.setCreateTime(now);
        memberStoreMapper.insert(newMemberStore);
    }
}
